{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from multiprocessing import Process, Queue, Pool, Manager, Pipe\n",
    "\n",
    "\n",
    "# def producer(queue):\n",
    "#     queue.put(\"a\")\n",
    "#     time.sleep(2)\n",
    "#\n",
    "# def consumer(queue):\n",
    "#     time.sleep(2)\n",
    "#     data = queue.get()\n",
    "#     print(data)\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     queue = Queue(10)\n",
    "#     my_producer = Process(target=producer, args=(queue,))\n",
    "#     my_consumer = Process(target=consumer, args=(queue,))\n",
    "#     my_producer.start()\n",
    "#     my_consumer.start()\n",
    "#     my_producer.join()\n",
    "#     my_consumer.join()\n",
    "\n",
    "#共享全局变量通信\n",
    "#共享全局变量不能适用于多进程编程，可以适用于多线程\n",
    "\n",
    "\n",
    "# def producer(a):\n",
    "#     a += 100\n",
    "#     time.sleep(2)\n",
    "#\n",
    "# def consumer(a):\n",
    "#     time.sleep(2)\n",
    "#     print(a)\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     a = 1\n",
    "#     my_producer = Process(target=producer, args=(a,))\n",
    "#     my_consumer = Process(target=consumer, args=(a,))\n",
    "#     my_producer.start()\n",
    "#     my_consumer.start()\n",
    "#     my_producer.join()\n",
    "#     my_consumer.join()\n",
    "\n",
    "#multiprocessing中的queue不能用于pool进程池\n",
    "#pool中的进程间通信需要使用manager中的queue\n",
    "\n",
    "# def producer(queue):\n",
    "#     queue.put(\"a\")\n",
    "#     time.sleep(2)\n",
    "#\n",
    "# def consumer(queue):\n",
    "#     time.sleep(2)\n",
    "#     data = queue.get()\n",
    "#     print(data)\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     queue = Manager().Queue(10)\n",
    "#     pool = Pool(2)\n",
    "#\n",
    "#     pool.apply_async(producer, args=(queue,))\n",
    "#     pool.apply_async(consumer, args=(queue,))\n",
    "#\n",
    "#     pool.close()\n",
    "#     pool.join()\n",
    "\n",
    "#通过pipe实现进程间通信\n",
    "#pipe的性能高于queue\n",
    "\n",
    "# def producer(pipe):\n",
    "#     pipe.send(\"bobby\")\n",
    "#\n",
    "# def consumer(pipe):\n",
    "#     print(pipe.recv())\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     recevie_pipe, send_pipe = Pipe()\n",
    "#     #pipe只能适用于两个进程\n",
    "#     my_producer= Process(target=producer, args=(send_pipe, ))\n",
    "#     my_consumer = Process(target=consumer, args=(recevie_pipe,))\n",
    "#\n",
    "#     my_producer.start()\n",
    "#     my_consumer.start()\n",
    "#     my_producer.join()\n",
    "#     my_consumer.join()\n",
    "\n",
    "def add_data(p_dict, key, value):\n",
    "    p_dict[key] = value\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    progress_dict = Manager().dict()\n",
    "    from queue import PriorityQueue\n",
    "\n",
    "    first_progress = Process(target=add_data, args=(progress_dict, \"bobby1\", 22))\n",
    "    second_progress = Process(target=add_data, args=(progress_dict, \"bobby2\", 23))\n",
    "\n",
    "    first_progress.start()\n",
    "    second_progress.start()\n",
    "    first_progress.join()\n",
    "    second_progress.join()\n",
    "\n",
    "    print(progress_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
